﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using qlqa.Delegate.CommonDelegate;
using qlqa.Delegate.FoodDelegate;
using qlqa.Delegate.ThucDonDelegate;
using qlqa.Models.Form.FoodForm;
using qlqa.Models.Form.ThucDonForm;
using qlqa.Models.LinQ;
using Webdiyer.WebControls.Mvc;

namespace qlqa.Controllers
{
    public class FoodController : Controller
    {
        ThucDonDelegate _thucDonDelegate = new ThucDonDelegate();
        FoodDelegate _foodDelegate = new FoodDelegate();
        CommonDelegate _commonDelegate = new CommonDelegate();
        //
        // GET: /Food/

        public ActionResult QuanLyMonAn()
        {
            return View();
        }

        public ActionResult GetDanhSachThucDon(string timKiem ,int? pageNumber)
        {
            var form = new ThucDonForm();
            var listThucDon = _thucDonDelegate.GetDanhSachThucDon(timKiem);
            form.ListThucDon = listThucDon;
            return View(form);
        }


        public ActionResult GetDanhSachMonAn(string timKiem, int? thucDonId, int? pageNumber)
        {
            var form = new FoodForm();
            var listMonAn = _foodDelegate.GetFoodByType(timKiem,thucDonId);
            form.DanhSachMonAn = new PagedList<tb_MonAn>(listMonAn, pageNumber ?? 1,5);
            return View(form);
        }

        public bool TaoMonAn(tb_MonAn monAn, HttpPostedFileBase uploadControl)
        {
            var ketQua = false;
            try
            {
                monAn.NgayTao = DateTime.Now;
                if (uploadControl != null)
                {
                    monAn.Hinh = uploadControl.FileName;
                }
                ketQua = _foodDelegate.InsertFood(monAn);
                if (ketQua)
                {
                    if (uploadControl != null)
                    {
                        string directoryPath =
                         Server.MapPath(_commonDelegate.GetPathForUploadFileAll("MonAn", null,
                             Convert.ToString(monAn.Id),
                             monAn.NgayTao != null ? monAn.NgayTao.Value : DateTime.Now));
                        if (!Directory.Exists(directoryPath))
                            Directory.CreateDirectory(directoryPath);
                        uploadControl.SaveAs(Path.Combine(directoryPath, uploadControl.FileName));
                        ketQua = true;
                    }
                }
            }
            catch (Exception)
            {

                ketQua = false;
            }
        
            return ketQua;
        }


        public JsonResult GetInfoMonAn(int idMonAn)
        {
            var monAn = _foodDelegate.GetFoodById(idMonAn);
            var path = _commonDelegate.GetPathForShowFileAll("MonAn", null, Convert.ToString(monAn.Id), monAn.NgayTao != null ? monAn.NgayTao.Value : DateTime.Now);
            monAn.Hinh = path + "/" + monAn.Hinh;
            return Json(monAn);
        }

        public bool CapNhatMonAn(tb_MonAn monAn, HttpPostedFileBase uploadControl)
        {
            var ketQua = false;
            try
            {
                monAn.NgayTao = DateTime.Now;
                if (uploadControl != null)
                {
                    monAn.Hinh = uploadControl.FileName;
                }
                else
                {
                    monAn.Hinh = string.Empty;
                }
                ketQua = _foodDelegate.UpdateFood(monAn);
                if (ketQua)
                {
                    if (uploadControl != null)
                    {
                        string directoryPath =
                         Server.MapPath(_commonDelegate.GetPathForUploadFileAll("MonAn", null,
                             Convert.ToString(monAn.Id),
                             monAn.NgayTao != null ? monAn.NgayTao.Value : DateTime.Now));
                        if (!Directory.Exists(directoryPath))
                            Directory.CreateDirectory(directoryPath);
                        uploadControl.SaveAs(Path.Combine(directoryPath, uploadControl.FileName));
                        ketQua = true;
                    }
                }
            }
            catch (Exception)
            {

                ketQua = false;
            }

            return ketQua;
        }


        public bool DeleteMonAn(int idMonAn)
        {
            var ketQua = _foodDelegate.DeleteFood(idMonAn);
            return ketQua;
        }

    }
}
